/*
 * Gavin Greene
 * 5/26/2011
 * 
 * This class implements the BusinessRule class. for more information, see Business Rule.
 */
package businessRule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import data.ClassSlot;
import data.TimeSlot;

/**
 * 
 * @author Gavin Greene
 * @version 5/26/2011
 * 
 * This class implements the method for the main method for business rule class superclass.
 * This class is meant to check the condition that a schedule has no 
 * teacher teaching two classes at the same time and on the same day.
 *
 */
public class TeacherTwoClassesAtOnce extends BizRule {


	/**
	 * {@inheritDoc}
	 */
	@Override
	public void CheckRule(final List<ClassSlot> theSchedule,
			Collection<BusinessRuleViolation> BusinessRulesViolated) {
		
		HashMap<String, List<TimeSlot>> teacherMap = new HashMap<String, List<TimeSlot>>();

		/* construct map from schedule */
		for (ClassSlot course : theSchedule){
			/* loop temp vars */ 
			TimeSlot courseTime = course.getTimeSlot();
			String currentTeacher = course.getClassTeacher();
			if(teacherMap.containsKey(currentTeacher)){
				List<TimeSlot> currentTeachersTimes = teacherMap.get(currentTeacher);
				/* check to see if the time assigned to the teacher is already on that teacher's list */ 
				if(currentTeachersTimes.contains(courseTime)){
// 				if(currentTeachersTimes.contains(courseTime) && courseTime != 'tba'){
					/* the teacher is already assigned to teach at that time */
					BusinessRulesViolated.add(new BusinessRuleViolation(currentTeacher, this));
				}
				else{
					/* add a time to the teacher's list */ 
					currentTeachersTimes.add(courseTime);
				}
			}
			else{
				/* we are adding a teacher to the map for the first time */
				ArrayList<TimeSlot> tempTeacherTimeBlock =  new ArrayList<TimeSlot>();
				tempTeacherTimeBlock.add(courseTime);
				teacherMap.put(currentTeacher, tempTeacherTimeBlock );
			}
		}
	}

	@Override
	public String getName() {
		return "Teacher is Teaching Two Classes at the same time";
	}

}
